<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的借阅</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">

    <link rel="stylesheet" href="css/list.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script src="js/jq-paginator.js"></script>
    
</head>

<body>
    <div class="bookContainer">
        <h2>我的借阅</h2>
        <div class="navbar-justify-between">
            <div>
                <button class="btn btn-outline-info" type="button" onclick="location.href='bookList.html'">返回</button>
                <button class="btn btn-outline-info" type="button" onclick="borrow()">归还选中</button>
            </div>
        </div>

        <table>
            <thead>
                <tr>
                    <td>选择</td>
                    <td class="width100">图书ID</td>
                    <td>书名</td>
                    <td>作者</td>
                    <td>定价</td>
                    <td>出版社</td>
                    <td>借阅时间</td>
                    <td class="width200">操作</td>
                </tr>
            </thead>
            <tbody>

            </tbody>
        </table>

        <div class="demo">
            <ul id="pageContainer" class="pagination justify-content-center"></ul>
        </div>
        <script src="js/common.js"></script>
        <script>
            getRecord();
            function getRecord() {
                $.ajax({
                    url: "/borrow/getRecord" + location.search,
                    type: "get",
                    success: function (result) {
                        if(result.code == "SUCCESS") {
                            var newhtml = "";
                            for (var record of result.data) {
                                newhtml += '<tr>';
                                newhtml += '<td><input type="checkbox" name="selectBook" value="' + record.id + '" id="selectBook" class="book-select"></td>';
                                newhtml += '<td>' + record.bookId + '</td>';
                                newhtml += '<td>' + record.bookName + '</td>';
                                newhtml += '<td>' + record.author + '</td>';
                                newhtml += '<td>' + record.price + '</td>';
                                newhtml += '<td>' + record.publish + '</td>';
                                newhtml += '<td>' + record.createTime + '</td>';
                                newhtml += '<td>';
                                newhtml += '<div class="op">';
                                // newhtml += '<a href="updateBook.html?bookId=' + bookInfo.id + '">修改</a>';
                                // newhtml += '<a href="javascript:void(0)" onclick="deleteBook(' + bookInfo.id + ')">删除</a>';
                                newhtml += '<a href="javascript:void(0)" onclick="returnBook(' + record.id + ')">归还</a>';
                                newhtml += '</div></td></tr>';
                            }
                            $("tbody").html(newhtml);
                        }else {
                            alert(result.errMsg);
                        }


                        //翻页信息
                        $("#pageContainer").jqPaginator({
                            totalCounts: 100, //总记录数
                            pageSize: 10,    //每页的个数
                            visiblePages: 5, //可视页数
                            currentPage: 1,  //当前页码

                            first: '<li class="page-item"><a class="page-link">首页</a></li>',
                            prev: '<li class="page-item"><a class="page-link" href="javascript:void(0);">上一页<\/a><\/li>',
                            next: '<li class="page-item"><a class="page-link" href="javascript:void(0);">下一页<\/a><\/li>',
                            last: '<li class="page-item"><a class="page-link" href="javascript:void(0);">最后一页<\/a><\/li>',
                            page: '<li class="page-item"><a class="page-link" href="javascript:void(0);">{{page}}<\/a><\/li>',
                            //页面初始化和页码点击时都会执行
                            onPageChange: function (page, type) {
                                //console.log(bookInfos.pageRequest.pageNumber);
                                console.log("第" + page + "页, 类型:" + type);
                                if (type == "change") {
                                    location.href = "myBorrow.html?pageNumber=" + page;
                                }
                            }
                        });
                    },
                    error:function(error) {
                        if (error != null && error.status == 401) {
                            alert("用户未登录");
                            location.href = "login.html";
                        }
                    }
                });
            }



            function returnBook(id) {
                urlParams = new URLSearchParams(window.location.search);
                $.ajax({
                    url: "/borrow/returnBook",
                    type: "post",
                    data: {
                        recordId: id
                    },
                    success: function (result) {
                        if (result.code == "SUCCESS") {
                            alert("归还成功");
                            location.href = "myBorrow.html" + location.search;
                        } else {
                            alert(result.errMsg);
                        }
                    },
                    error: function (error) {
                        if (error != null && error.status == 401) {
                            alert("用户未登录");
                            location.href = "login.html";
                        }
                    }
                });
            }
            function borrows() {
                //获取复选框的id
                var ids = [];
                $("input:checkbox[name='selectBook']:checked").each(function () {
                    ids.push($(this).val());
                });
                console.log(ids);
                $.ajax({
                    url: "/book/borrow?ids=" + ids,
                    type: "post",
                    success: function (result) {
                        if (result.code == "SUCCESS") {
                            alert("成功借阅" + result.data + "本书");
                            location.href = "bookList.html";
                        } else {
                            alert(result.errMsg);
                        }
                    },
                    error: function (error) {
                        if (error != null && error.status == 401) {
                            alert("用户未登录");
                            location.href = "login.html";
                        }
                    }
                });
            }

        </script>
    </div>
</body>

</html>